package org.tedu.weibo.common.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class LogAspect {
    @Before("execution(public org.tedu.weibo.common.result.JsonResult org.tedu.weibo.controller.*.*(..))")
    public void before(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getName();
        log.info(className + "." + methodName + "()开始执行");
    }

    @AfterReturning(
            value = "execution(public org.tedu.weibo.common.result.JsonResult org.tedu.weibo.controller.*.*(..))",
            returning = "result"
    )
    public void afterReturning(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getName();
        log.info(className + "." + methodName + "()执行结束");
    }

    @AfterThrowing(
            value = "execution(public org.tedu.weibo.common.result.JsonResult org.tedu.weibo.controller.*.*(..))",
            throwing = "e"
    )
    public void afterThrowing(JoinPoint joinPoint, Throwable e) {
        log.error(e.getMessage(), e);
    }

    @After("execution(public org.tedu.weibo.common.result.JsonResult org.tedu.weibo.controller.*.*(..))")
    public void after(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getName();
        log.info(className + "." + methodName + "()执行完毕");
    }
}
